<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform  ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		hasEditor,
		getEditorTitle,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				userCacheKey:"per-zg-subjectiveEvaluate-user-cache",
				evaluateMainUserCacheKey:'per-zg-subjectiveEvaluate-evaluateMainUser-cache',
				evaluateUserCacheKey:"per-zg-subjectiveEvaluate-evaluateUser-cache",
				list:[],
				minNum:'',
				maxNum:''
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			verifyEvaluateMainUser(){
				let _that = this
				if(!_that.$refs.autoform.formdata.userId){
					let title = _that.$refs.autoform.getEditorTitle("userTitle","userId")
					_that.om.toast("请先选择"+title)
					return false
				}else{
					return true
				}
			},
			verifyEvaluateUser(){
				let _that = this
				if(!_that.$refs.autoform.formdata.userId){
					let title = _that.$refs.autoform.getEditorTitle("userTitle","userId")
					_that.om.toast("请先选择"+title)
					return false
				}else{
					return true
				}
			},
			getUserNumber(){
				let _that = this
				_that.om.request(
					"subjectiveEvaluate/getUserParam"
				).then(res=>{
					if(!_that.$refs.autoform.formdata.evaluateUserIdStr){
						_that.om.toast("请选择"+res.value.userMinNum +'-' + res.value.userMaxNum+"个人员进行评价")
					}else{
						let idList = _that.$refs.autoform.formdata.evaluateUserIdStr.split(",")
						if(idList.length<res.value.userMinNum || idList.length>res.value.userMaxNum){
							_that.om.toast("请选择"+res.value.userMinNum +'-' + res.value.userMaxNum+"个人员进行评价")
							_that.$refs.autoform.clear("evaluateTitle","evaluateUserIdStr")
						}
					}
					
				})	
			},
			formSubmit(){
				let _that = this 
				if(_that.edit){
					_that.om.formSave("subjectiveEvaluate/doUpdate",_that.formdata,_that)
				}else if(_that.checkedit){
					_that.om.formSave("subjectiveEvaluate/doReviewUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("subjectiveEvaluate/doAdd",_that.formdata,_that)
				}
			},
			findItemUserList(list){
				let _that = this
				_that.om.request(
					"subjectiveEvaluate/findItemUserList",
					{
						userIdStr:_that.$refs.autoform.formdata.evaluateUserIdStr
					},
					"post"
				).then(res=>{
					if(res.value){
						for(let item of list){
							item.itemUserScore = res.value.map(e=>e.text +": 未填写").join('\n')
							item.userList =  res.value.map(e=>{return {"evaluateUserId":e.id}})
						}
						_that.subEditorObj["itemTitle"].workList = list
					}
				})
			},	
			changeEvaluateUserType(){
				let _that = this 
				_that.$refs.autoform.changeEditor("evaluateTitle","evaluateUserIdStr",{
					link:"../../public/multiSearch/multiSearch",
					"linkParam":{
						other:JSON.stringify(),
						url:"temployee/getCombotreeUserList",
						initload:true,
						showData:false,
						cachename:_that.evaluateUserCacheKey,
						namefield:"text",
						placeholder:"请输入姓名"
					}
				})
			},
			initSelect(iList){
				let _that = this
				let objArr = []
				let idList = iList.split(",")
				for(let index in idList){
					let obj = {
						id:idList[index]
					}
					objArr.push(obj)
				}
				_that.$refs.autoform.changeEditor('evaluateTitle', 'evaluateUserIdStr', {
					initSelect: objArr
				})
			},
			changeUserIdType(){
				let _that = this
				_that.$refs.autoform.changeEditor("evaluateTitle","evaluateMainUserPost",{
					link:"../../public/pageSearch/pageSearch",
					"linkParam":{
						other:JSON.stringify({
							firstUserId:_that.$refs.autoform.formdata.evaluateMainUserId?_that.$refs.autoform.formdata.evaluateMainUserId:''
						}),
						url:"orgInstPosUserOfficial/findPagerByOrgId",
						initload:true,
						cachename:_that.evaluateMainUserCacheKey,
						namefield:"userPosName",
						extrafield:'instName',
						placeholder:"请输入姓名"
					}
				})
			},
			getInfo1(){
				let _that = this
				_that.om.request(
					"tworkInfo/getInfo1",
					{
						userId:_that.$refs.autoform.formdata.userId
					}
				).then(res=>{
					if(_that.subEditorObj["postTitle"]){
						_that.subEditorObj["postTitle"].workList = res.value
					}
				})
			},	
			clearUserId(){
				let _that = this
				_that.$refs.autoform.clearGroup("userTitle")
				_that.$refs.autoform.clearGroup("postTitle")
				_that.$refs.autoform.clearGroup("evaluateTitle")
				_that.$refs.autoform.clearGroup("itemTitle")
			},
			verifyUserId(){
				let _that = this
				if(!_that.$refs.autoform.formdata.year){
					let title = _that.$refs.autoform.getEditorTitle("topinfo","year")
					_that.om.toast("请先选择"+title)
					return false
				}else{
					return true
				}
			},
			getEmpBasicInfo(){
				let _that = this
				_that.om.request(
					"temployee/getEmpBasicInfo",
					{
						userId:_that.$refs.autoform.formdata.userId
					}
				).then(res=>{
					_that.$refs.autoform.formdata.sexName = res.value.sexName
					_that.$refs.autoform.formdata.birthdate = res.value.birthdate
					_that.$refs.autoform.formdata.birthPlace = res.value.birthPlace
					_that.$refs.autoform.formdata.highestEduName = res.value.highestEduName
					_that.$refs.autoform.formdata.entrydate = res.value.entrydate
					_that.$refs.autoform.formdata.mobile = res.value.mobile
					_that.$refs.autoform.formdata.areaName = res.value.areaName
					_that.$refs.autoform.formdata.idTypeName = res.value.idTypeName
					_that.$refs.autoform.formdata.idcard = res.value.idcard
					_that.$refs.autoform.formdata.certificateValidDate = res.value.certificateValidDate
					_that.$refs.autoform.formdata.naitionName = res.value.naitionName
					_that.$refs.autoform.formdata.maritalstatusName = res.value.maritalstatusName
					_that.$refs.autoform.formdata.polticsName = res.value.polticsName
					_that.$refs.autoform.formdata.bloodTypeName = res.value.bloodTypeName
					_that.$refs.autoform.formdata.userno = res.value.userno
					_that.$refs.autoform.formdata.registerCategoryName = res.value.registerCategoryName
					_that.$refs.autoform.formdata.birthaddress = res.value.birthaddress
					_that.$refs.autoform.formdata.currentaddress = res.value.currentaddress
				})
			},
			getAutoLoadData(userId){
				let _that = this
				_that.om.request(
					"subjectiveEvaluate/getAutoLoadData",
					{
						userId:_that.$refs.autoform.formdata.userId
					}
				).then(res=>{
					if(res.value.lastData){
						_that.$refs.autoform.formdata.evaluateMainUserId = res.value.lastData.evaluateMainUserId
						_that.$refs.autoform.formdata.evaluateMainUserName = res.value.lastData.evaluateMainUserName
						_that.$refs.autoform.formdata.evaluateMainPostId = res.value.lastData.evaluateMainPostId
						_that.$refs.autoform.formdata.evaluateMainPostName = res.value.lastData.evaluateMainPostName
						_that.$refs.autoform.formdata.evaluateMainInstId = res.value.lastData.evaluateMainInstId
						_that.$refs.autoform.formdata.evaluateMainInstName = res.value.lastData.evaluateMainInstName
						let iList = []
						if(res.value.lastData.evaluateUserIdStr){
							iList = res.value.lastData.evaluateUserIdStr.split(",")
						}
						_that.$refs.autoform.formdata.evaluateUserIdStr = iList
						let nList = []
						if(res.value.lastData.evaluateUserNameStr){
							nList = res.value.lastData.evaluateUserNameStr.split(",")
						}
						
						let nameList = []
						for(let item of nList){
							let nArr = item.split("/")
							nameList.push(nArr[0])
						}
						_that.$refs.autoform.formdata.evaluateUserNameStr = nameList.toString()
						if(res.value.lastData.evaluateUserIdStr,res.value.lastData.evaluateUserNameStr){
							_that.initSelect(res.value.lastData.evaluateUserIdStr,res.value.lastData.evaluateUserNameStr)
						}
					}
					if(res.value.itemList){
						_that.findItemUserList(res.value.itemList)
					}
					_that.changeEvaluateUserType()
					_that.changeUserIdType()
				})
			},
			canAddData(){
				let _that = this
				_that.om.request(
					"subjectiveEvaluate/canAddData",
					{
						year:_that.$refs.autoform.formdata.year,
						month:_that.$refs.autoform.formdata.month,
						userId:_that.$refs.autoform.formdata.userId
					}
				).then(res=>{
					if(res.succeed && res.value){
						_that.getAutoLoadData()
						_that.getEmpBasicInfo()
						_that.getInfo1()
					}else{
						errorModal(_that,"提示信息",ignoreHtml(res.msg),()=>{
							_that.$refs.autoform.clearGroup("userTitle")
							_that.$refs.autoform.clearGroup("evaluateTitle")
						})
					}
				})
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:"common/findYearList",
						param:{
							berfore:'1',
							after:'0',
							oldYear:''
						}
					},
					{
						url:"common/findMonthList",
					}
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"subjectiveEvaluate/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id",'posname'),mainData)
					changeEditor("topinfo","year","data",configSelectData(res[1],"name","value"),mainData)
					changeEditor("topinfo","month","data",configSelectData(res[2],"referNum","dicname"),mainData)
					changeEditor("userTitle","userId",{
						link:"../../public/search/search",
						"linkParam":{
							other:JSON.stringify({}),
							url:"temployee/getCombotreeUserList",
							cachename:_that.userCacheKey,
							initload:true,
							showData:false,
							namefield:"text",
							placeholder:"请输入姓名"
						}
					},null,mainData)
					if(_that.subEditorObj["postTitle"]){
						let att = {
							showDel:false,
							showAdd:false,
							showArrow:false
						}
						let configData = _that.subConfigData["postTitle"]
						if(hasEditor("postTitle","posName",configData)){
							att.titleTemplate = "<%= posName || '未填写'%>"
						}
						if(hasEditor("postTitle","salaryPosFlagStr",configData)){
							if(att.titleTemplate){
								att.titleTemplate = att.titleTemplate+"<%= salaryPosFlagStr %>"
							}
						}
						if(hasEditor("postTitle","parentUserName",configData)){
							let title = getEditorTitle("postTitle","parentUserName",configData)
							att.subtitle1Template = title+ ":<%= parentUserName || '未填写'%>"
						}
						if(hasEditor("postTitle","workWayName",configData)){
							let title = getEditorTitle("postTitle","workWayName",configData)
							if(att.subtitle1Template){
								att.subtitle1Template = att.subtitle1Template + ' | ' + title + ":<%= workWayName || '未填写'%>"
							}else{
								att.subtitle1Template = title + ":<%=workWayName || '未填写'%>"
							}
						}
						if(hasEditor("postTitle","instShort",configData)){
							let title = getEditorTitle("postTitle","instShort",configData)
							if(att.subtitle1Template){
								att.subtitle1Template = att.subtitle1Template + ' | ' + title + ":<%= instShort || '未填写'%>"
							}else{
								att.subtitle1Template = title + ":<%= instShort || '未填写'%>"
							}
						}
						if(hasEditor("postTitle","parentInst",configData)){
							let title = getEditorTitle("postTitle","parentInst",configData)
							att.subtitle2Template = title+ ":<%=parentInst || '未填写'%>"
						}
						if(hasEditor("postTitle","posCareercat",configData)){
							let title = getEditorTitle("postTitle","posCareercat",configData)
							if(att.subtitle2Template){
								att.subtitle2Template += " | " + title+ ":<%=posCareercat || '未填写'%>"
							}else{
								att.subtitle2Template = title+ ":<%=posCareercat || '未填写'%>"
							}
						}
						if(hasEditor("postTitle","posLevel",configData)){
							let title = getEditorTitle("postTitle","posLevel",configData)
							if(att.subtitle2Template){
								att.subtitle2Template += " | " + title+ ":<%=posLevel || '未填写'%>"
							}else{
								att.subtitle2Template = title+ ":<%=posLevel || '未填写'%>"
							}
						}
						if(hasEditor("postTitle","salaryWay",configData)){
							let title = getEditorTitle("postTitle","salaryWay",configData)
							att.subtitle3Template = title+ ":<%=salaryWay || '未填写'%>"
						}
						if(hasEditor("postTitle","attributeName",configData)){
							let title = getEditorTitle("postTitle","attributeName",configData)
							if(att.subtitle3Template){
								att.subtitle3Template += ' | '+title+ ":<%=attributeName || '未填写'%>"
							}else{
								att.subtitle3Template = title+ ":<%=attributeName || '未填写'%>"
							}
						}
						
						if(hasEditor("postTitle","workDate",configData)){
							let title = getEditorTitle("postTitle","workDate",configData)
							if(att.subtitle3Template){
								att.subtitle3Template += ' | '+title+ ":<%=workDate || '未填写'%>"
							}else{
								att.subtitle3Template = title+ ":<%=workDate || '未填写'%>"
							}
						}
						if(hasEditor("postTitle","probationPosFlagStr",configData)){
							let title = getEditorTitle("postTitle","probationPosFlagStr",configData)
							if(att.subtitle3Template){
								att.subtitle3Template += ' | '+title+ ":<%=probationPosFlagStr || '未填写'%>"
							}else{
								att.subtitle3Template = title+ ":<%=probationPosFlagStr || '未填写'%>"
							}
						}
						changeEditor("postTitle","postTitle",att,mainData)
					}
					if(_that.subEditorObj["itemTitle"]){
						let att = {
							showAdd:false,
							showDel:false
						}
						let configData = _that.subConfigData["itemTitle"]
						if(hasEditor("itemTitle","itemName",configData)){
							att.titleTemplate = "<%= itemName || '未填写'%>"
						}
						if(hasEditor("itemTitle","standardScore",configData)){
							let title  = getEditorTitle("itemTitle","standardScore",configData)
							att.subtitle1Template =title+ ":<%= standardScore || '未填写'%>"
						}
						if(hasEditor("itemTitle","parentWeight",configData)){
							let title = getEditorTitle("itemTitle","parentWeight",configData)
							if(att.subtitle1Template){
								att.subtitle1Template += " | " + title + ":<%= parentWeight || '未填写'%>"
							}else{
								att.subtitle1Template = title + "<%= parentWeight || '未填写'%>"
							}
						}
						if(hasEditor("itemTitle","itemUserScore",configData)){
							let title = getEditorTitle("itemTitle","itemUserScore",configData)
							att.subtitle2Template = title + ":<%= itemUserScore || '未填写'%>"
						}
						if(hasEditor("itemTitle","selfEvaluateScore",configData)){
							let title = getEditorTitle("itemTitle","selfEvaluateScore",configData)
							if(att.subtitle2Template){
								att.subtitle2Template += " | "+title + ":<%= selfEvaluateScore || '未填写'%>"
							}else{
								att.subtitle2Template = title + ":<%= selfEvaluateScore || '未填写'%>"
							}
						}
						if(hasEditor("itemTitle","singleScore",configData)){
							let title = getEditorTitle("itemTitle","singleScore",configData)
							att.subtitle3Template = title + ":<%= singleScore || '未填写' %>"
						}
						if(hasEditor("itemTitle","resultName",configData)){
							let title = getEditorTitle("itemTitle","resultName",configData)
							if(att.subtitle3Template){
								att.subtitle3Template += ' | '+title + ":<%= resultName || '未填写'%>"
							}else{
								att.subtitle3Template = title + ":<%= resultName || '未填写'%>"
							}
						}
						changeEditor("itemTitle","dataStr",att,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[3].value.subjectiveEvaluate){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[3].value.subjectiveEvaluate[index])
								_that.$set(_that.formdata,index,res[3].value.subjectiveEvaluate[index])
							}
						}
						for(let index in res[3].value.temployeeResponse){
							if(fieldArr.includes(index) && index != "id" ){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[3].value.temployeeResponse[index])
								_that.$set(_that.formdata,index,res[3].value.temployeeResponse[index])
							}
						}
						let postList = _that._.cloneDeep(res[3].value.tworkInfos)
						_that.subEditorObj["postTitle"].workList = postList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'postTitle',JSON.stringify(postList))
						_that.$set(_that.$refs.autoform.formdata,"postTitle",JSON.stringify(postList))
						
						let itemList = _that._.cloneDeep(res[3].value.itemList)
						let nameList = _that.$refs.autoform.formdata.evaluateUserNameStr?_that.$refs.autoform.formdata.evaluateUserNameStr.split(","):''
						if(nameList){
							for(let tItem of itemList){
								if(tItem.selfEvaluateScore == 0){
									tItem.selfEvaluateScore = "0"
								}
								if(tItem.singleScore == 0){
									tItem.singleScore = "0"
								}
								if(!tItem.itemUserScore){
									tItem.itemUserScore = nameList.map(e=>e +": 未填写").join('\n')
								}else{
									tItem.itemUserScore = tItem.itemUserScore.split("、").map(e=>e).join('\n')
								}
							}
						}
						_that.subEditorObj["itemTitle"].workList = itemList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'dataStr',JSON.stringify(postList))
						_that.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(postList))
						changeEditor("topinfo","zposId","type","labelbox",mainData)
						changeEditor("topinfo","year","type","labelbox",mainData)
						changeEditor("topinfo","month","type","labelbox",mainData)
						changeEditor("userTitle","userId","type","labelbox",mainData)
						_that.$nextTick(function(){
							_that.changeUserIdType()
							_that.changeEvaluateUserType()
							_that.initSelect(_that.$refs.autoform.formdata.evaluateUserIdStr,_that.$refs.autoform.formdata.evaluateUserNameStr)
						})
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}
						if(_that.checkedit){
							_that.$refs.autoform.changeEditor("resultTitle","evaluateRemark","type","textarea")
							_that.$refs.autoform.changeEditor("resultTitle","evaluateRemark","prompt","请输入")
						}
					})
					
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.id
							if(d.text.indexOf("/")){
								formdata.userName = d.text.split("/")[0]
							}
							_that.$refs.autoform.clearGroup("postTitle")
							_that.$refs.autoform.clearGroup("evaluateTitle")
							if(_that.subEditorObj["itemTitle"]){
								_that.subEditorObj["itemTitle"].workList = []
							}
							_that.canAddData()
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.evaluateMainUserCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							_that.$refs.autoform.$set(formdata,'evaluateMainUserId',d.userId)
							formdata.evaluateMainUserName = d.userName
							formdata.evaluateMainPostId = d.posId
							formdata.evaluateMainPostName = d.posName
							formdata.evaluateMainInstId = d.instId
							formdata.evaluateMainInstName = d.instName
							uni.removeStorage({
								key:_that.evaluateMainUserCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.evaluateUserCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.evaluateUserIdStr = d.map(e=>e.id).join(",")
							let nList = d.map(e=>e.text)
							let nList1 = d.map(e=>e.text).join(",")
							let nameList = []
							for(let item of nList){
								let nArr = item.split("/")
								nameList.push(nArr[0])
							}
							formdata.evaluateUserNameStr = nameList.toString()
							for(let tItem of _that.subEditorObj["itemTitle"].workList){
								tItem.itemUserScore = nameList.map(e=>e +": 未填写").join('\n')
								tItem.userList =  d.map(e=>{return {"evaluateUserId":e.id}})
							}
							_that.initSelect(formdata.evaluateUserIdStr,nList1)
							_that.getUserNumber()
							uni.removeStorage({
								key:_that.evaluateUserCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
